<template>
  <div>
    <div style="margin-top: 20px">
      <el-button @click="handleOpenDialog">添加</el-button>
    </div>
    <el-table
      ref="listTableRef"
      :data="listTableData"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column label="日期" width="120">
        <template slot-scope="scope">{{ scope.row.date }}</template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="120"> </el-table-column>
      <el-table-column prop="address" label="地址" show-overflow-tooltip>
      </el-table-column>
      <el-table-column property="address" label="操作">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.row)" type="text" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="收货地址" :visible.sync="dialogTableVisible">
      <el-table
        :data="dialogTableData"
        ref="dialogTableRef"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
          property="date"
          label="日期"
          width="150"
        ></el-table-column>
        <el-table-column
          property="name"
          label="姓名"
          width="200"
        ></el-table-column>
        <el-table-column property="address" label="地址"></el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="5"
        :total="7"
      >
      </el-pagination>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmDialog">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      listTableData: [],
      dialogTableData: [
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          id: 1,
        },
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          id: 2,
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          id: 3,
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          id: 4,
        },
        {
          date: "2016-05-08",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          id: 5,
        },
      ],
      dialogTableVisible: false,
      dialogTableSelection: [],
      realTempSelect: [],
    };
  },

  methods: {
    handleOpenDialog() {
      this.dialogTableVisible = true;
      this.realTempSelect = this.dialogTableSelection;
      this.$nextTick(() => {
        this.$refs["dialogTableRef"].clearSelection();
        this.realTempSelect.forEach((item) => {
          this.$refs["dialogTableRef"].toggleRowSelection(item, true);
        });
      });
    },
    handleConfirmDialog() {
      this.listTableData = [
        ...this.listTableData,
        ...this.dialogTableSelection.filter(
          (item) => !this.listTableData.some((i) => i.id === item.id)
        ),
      ];
      this.dialogTableVisible = false;
    },
    handleSelectionChange(val) {
      this.dialogTableSelection = val;
    },
    handleDelete(row) {
      this.listTableData = this.listTableData.filter(
        (item) => item.id !== row.id
      );
      if (this.dialogTableSelection.length) {
        this.dialogTableSelection = this.dialogTableSelection.filter(
          (item) => item.id !== row.id
        );
      }
    },
  },
};
</script>
